package com.zdong.hintview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * Created by user on 2015/10/13.
 */
public class BootBroadcastReceiver extends BroadcastReceiver {
    static final String action_boot = "android.intent.action.BOOT_COMPLETED";
    static final String USER_PRESENT = "android.intent.action.USER_PRESENT";
    static final String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
    static final String USER = "android.intent.action.USER_ZDONG";
    private static int jin = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("TAG", "接收广播" + intent.getAction());
        SharedPreferences sharedPreferences = context.getSharedPreferences("wel", Context.MODE_APPEND);
        if (intent.getAction().equals(action_boot) && jin == 0) {
            jin = 1;
            Intent ootStartIntent = new Intent(context, WelComeService.class);
            context.startService(ootStartIntent);
        } else  /* if (USER_PRESENT.equals(intent.getAction())){
            Log.e("TAG", SystemClock.elapsedRealtime() / 1000 + "s"+sharedPreferences.getBoolean("isshutdown", true));  //开机时间
            Log.e("TAG", System.nanoTime() / 1000 + "s"+sharedPreferences.getBoolean("isshutdown", true));  //开机时间
            float time=SystemClock.uptimeMillis() / 1000;
            if (sharedPreferences.getBoolean("isshutdown", true)&&time<1*60*1000) {
                Intent ootStartIntent=new Intent(context,WelComeService.class);
                context.startService(ootStartIntent);
            }
        }else*/
        /*if (ACTION_SHUTDOWN.equals(intent.getAction())) {
            sharedPreferences.edit().putBoolean("isshutdown", true).commit();
        } else */if (USER.equals(intent.getAction())) {
            boolean isopen = intent.getBooleanExtra("isopen", false);
            int count = sharedPreferences.getInt("count", 0);
            Log.e("TAG", "接收广播" + count);
            if (count > 0 && isopen && jin == 0) {
                jin = 1;
                Intent ootStartIntent = new Intent(context, WelComeService.class);
                context.startService(ootStartIntent);
            }
            if (!isopen) {
                sharedPreferences.edit().putInt("count", 0).commit();
            } else {
                sharedPreferences.edit().putInt("count", ++count).commit();
            }
        }

    }

}